﻿/*jslint browser: true, cap: false, passfail: false, undef: false, white: false */
/*global SONGHAY YAHOO */

(function() {

    //Module-level members:
    var application = SONGHAY.ui.GenericWebEditor;
    var client = SONGHAY.ui.Client;

    SONGHAY.ui.GenericWebEditor.displayBuilder =
    {
        forCalendar: function() {
            var oCalendarBlock = YAHOO.util.Dom.get('CalendarBlock');
            if (oCalendarBlock) { } else { return; }

            application.calendar =
                new YAHOO.widget.Calendar('SONGHAY.ui.GenericWebEditor.calendar', oCalendarBlock.id, { title: "Choose a date:", close: true });
            application.calendar.selectEvent.subscribe(application.calendarSetDate, application.calendar, true);
            application.calendar.render();
        },

        forCommandsOnLoad: function() {
            //Wire up command events:
            client.ui.EventCore.setEventByWidgetId('ChangeDataSource');
            client.ui.EventCore.setEventByWidgetId('Index');
            client.ui.EventCore.setEventByWidgetId('NewDocument');
            client.ui.EventCore.setEventByWidgetId('NewFragment');
            client.ui.EventCore.setEventByWidgetId('NewSegment');
        },

        forDefaultCall: function(oTarget) {
            if (oTarget && (oTarget.innerHTML !== undefined)) {
                oTarget.innerHTML = '<div class="AjaxMessage"><p><img alt="Sending request..." src="./js/songhay/assets/ajax-loader-large.gif" \/><\/p><p>Sending request&#8230;<\/p><\/div>';
            }
        },

        forDisplayBlockResponse: function(oTarget, vTargetContent) {
            if (oTarget) {
                oTarget.innerHTML = vTargetContent;
                application.buildDisplay();
            }
        },

        forFormsOnLoad: function() {
            var o = null;

            if (client.settings.DataConnectionId.value.length === 0) {
                o = YAHOO.util.Dom.get('CommandBlock');
                if (o) { YAHOO.util.Dom.setStyle(o.id, 'display', 'none'); }
            }
            else {
                o = YAHOO.util.Dom.get('CommandBlock');
                if (o) { YAHOO.util.Dom.setStyle(o.id, 'display', 'block'); }
            }

            //Format search widgets:
            client.ui.EventCore.setEventByWidgetId('Search');
            client.ui.EventCore.setEventByWidgetId('SearchText');
            o = YAHOO.util.Dom.get('SearchText');
            if (o) { o.value = 'Search...'; }

            //TODO: Format search commands:
        },

        forSearch: function() {
            var o = YAHOO.util.Dom.get('SearchResultsTable');
            var i;

            if (o) {
                client.ui.TablePageViewer.pageTable('SearchResultsTable', 'NavigationCaption');
                client.ui.EventCore.setEventByWidgetId('SearchResultsPreviousRows');
                client.ui.EventCore.setEventByWidgetId('SearchResultsNextRows');

                o = YAHOO.util.Dom.getElementsByClassName('SearchResultForDocument', 'a', 'SearchResultsTable');
                if (o && o.length) { for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('SearchResultForDocument', o[i]); } }

                o = YAHOO.util.Dom.getElementsByClassName('SearchResultForFragment', 'a', 'SearchResultsTable');
                if (o && o.length) { for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('SearchResultForFragment', o[i]); } }

                o = YAHOO.util.Dom.getElementsByClassName('SearchResultForSegment', 'a', 'SearchResultsTable');
                if (o && o.length) { for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('SearchResultForSegment', o[i]); } }
            }
        },

        forTextArea: function(vTextAreaId) {
            var o = null;

            //Format textarea elements:
            o = YAHOO.util.Dom.get(vTextAreaId);
            if (o) {
                if (o.innerHTML == '&#160;') { o.innerHTML = ''; }
                if (o.innerHTML == '&nbsp;') { o.innerHTML = ''; }

                try {
                    o.wrap = 'soft';
                }
                catch (e) { }
            }
        },

        forUlDropDownInit: function(vTargetId) {
            var oDropDown = YAHOO.util.Dom.get(vTargetId + 'UlDropDown');
            var oBlock = null;
            var o = null;
            var i = 0;

            if (oDropDown) { } else { return; }

            oBlock = YAHOO.util.Dom.get(vTargetId + 'Items');
            if (oBlock) { } else { return; }

            o = oBlock.getElementsByTagName('a');
            if (o && o.length) {
                for (i = 0; i < o.length; i++) {
                    client.ui.EventCore.setEventByMetaDataId(vTargetId + 'UlDropDownItems', o[i]);
                }
            }

            o = YAHOO.util.Dom.get(vTargetId);
            if (o && o.value) { oDropDown.initialValue = o.value; }

            o = YAHOO.util.Dom.get(vTargetId + 'SelectedItem');
            client.ui.EventCore.setEventByWidgetId(o.id);
            if (o) { oDropDown.initialDisplayName = o.innerHTML; }

            client.ui.EventCore.setEventByWidgetId(vTargetId + 'Cancel');
            client.ui.EventCore.setEventByWidgetId(vTargetId + 'Remove');
            client.ui.EventCore.setEventByWidgetId(vTargetId + 'Reset');
        },

        forWebFormDocument: function() {
            //Document stuff:
            var o = YAHOO.util.Dom.get('DocumentForm');
            var o2 = null;

            if (o) {
                client.ui.EventCore.setEventByWidgetId('DeleteDocument');
                client.ui.EventCore.setEventByWidgetId('SaveDocument');
                client.ui.EventCore.setEventByWidgetId('SaveNewDocument');

                self.forTextArea('Tag');
                self.forTextArea('Title');

                o2 = YAHOO.util.Dom.get('Title');
                if (o2) { o2.focus(); }
            }
        },

        forWebFormFragment: function() {
            var o = YAHOO.util.Dom.get('FragmentForm');
            var o2;

            if (o) {
                client.ui.EventCore.setEventByWidgetId('DeleteFragment');
                client.ui.EventCore.setEventByWidgetId('SaveFragment');
                client.ui.EventCore.setEventByWidgetId('SaveNewFragment');

                client.dataForm.formatTextArea('FragmentDisplayName');

                client.dataForm.formatTextArea('ProseFragment');

                o2 = YAHOO.util.Dom.get('FragmentName');
                if (o2) { o2.focus(); }
            }
        },

        forWebFormSegment: function() {
            //Segment stuff:
            var o = YAHOO.util.Dom.get('SegmentForm');
            var o2 = null;

            if (o) {
                self.forUlDropDownInit('ParentSegmentId');
                client.ui.EventCore.setEventByWidgetId('DeleteSegment');
                client.ui.EventCore.setEventByWidgetId('SaveSegment');
                client.ui.EventCore.setEventByWidgetId('SaveNewSegment');

                self.forTextArea('SegmentName');

                o2 = YAHOO.util.Dom.get('SegmentName');
                if (o2) { o2.focus(); }
            }
        },

        forWebForm: function() {
            var o = null;
            var i;

            //Look for the general class of DataEntryForm:
            o = YAHOO.util.Dom.getElementsByClassName('DataEntryForm', 'form', 'DisplayBlock');
            if (o && o.length) { } else { return; }

            //Generic stuff:
            self.forCalendar();

            client.ui.EventCore.setEventByWidgetId('CreateDate');
            client.ui.EventCore.setEventByWidgetId('EndDate');
            client.ui.EventCore.setEventByWidgetId('ModificationDate');

            //Other generic stuff:
            self.forUlDropDownInit('SegmentId');
            o = YAHOO.util.Dom.get('DataFormTabView');
            if (o) { o = new YAHOO.widget.TabView(o.id, { orientation: 'bottom' }); }

            self.forWebItems();

            //Format input[@type='checkbox'] elements:
            o = YAHOO.util.Dom.getElementsByClassName('CheckBox', 'input', 'DataFormTabView');
            if (o && o.length) { for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('CheckBox', o[i]); } }
        },

        forWebIndex: function() {
            var o = YAHOO.util.Dom.get('WebIndex');
            var i;

            if (o) {
                //Format Web index toggle widgets:
                o = YAHOO.util.Dom.getElementsByClassName('WebIndexTog', 'a', 'DisplayBlock');
                if (o && o.length) {
                    for (i = 0; i < o.length; i++) {
                        o[i].innerHTML = '<img src="./js/songhay/assets/cube-plus.gif" />';
                        client.ui.EventCore.setEventByMetaDataId('WebIndexTog', o[i]);
                    }
                }

                //Format Web index links:
                self.forWebItems();
            }
        },

        forWebItems: function() {
            var o = YAHOO.util.Dom.getElementsByClassName('DocumentLink', 'a', 'DisplayBlock');
            var i;

            if (o && o.length) {
                for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('DocumentLink', o[i]); }
            }

            o = YAHOO.util.Dom.getElementsByClassName('DocumentLink_Inactive', 'a', 'DisplayBlock');
            if (o && o.length) {
                for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('DocumentLink_Inactive', o[i]); }
            }

            o = YAHOO.util.Dom.getElementsByClassName('FragmentLink', 'a', 'DisplayBlock');
            var i;

            if (o && o.length) {
                for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('FragmentLink', o[i]); }
            }

            o = YAHOO.util.Dom.getElementsByClassName('FragmentLink_Inactive', 'a', 'DisplayBlock');
            if (o && o.length) {
                for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('FragmentLink_Inactive', o[i]); }
            }

            o = YAHOO.util.Dom.getElementsByClassName('SegmentLink', 'a', 'DisplayBlock');
            if (o && o.length) {
                for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('SegmentLink', o[i]); }
            }

            o = YAHOO.util.Dom.getElementsByClassName('SegmentLink_Inactive', 'a', 'DisplayBlock');
            if (o && o.length) {
                for (i = 0; i < o.length; i++) { client.ui.EventCore.setEventByMetaDataId('SegmentLink_Inactive', o[i]); }
            }
        }

    };

    //Module-level "this":
    var self = SONGHAY.ui.GenericWebEditor.displayBuilder;

})();
